def single(cls):
    instance = {}
    x = 1

    def getinstance():
        nonlocal x
        x += 1
        print(x)

        if cls not in instance:
            instance[cls] = cls()
        return instance[cls]

    return getinstance


def single2(cls):
    instance = ''
    x = 0

    def getinstance():
        nonlocal x
        nonlocal instance
        if x == 0:
            x = 1
            instance = cls()
        print(x)
        return instance

    return getinstance


def single3(cls):
    instance = ''

    def getinstance():
        nonlocal instance
        if instance == '':
            instance = cls()
        return instance

    return getinstance


@single3
class A:
    pass


a = A()
print(id(a))

b = A()
print(id(b))

print(a is b)